package com.bdq.interaction.application.service.impl;

import cn.hutool.core.convert.Convert;
import com.bdq.common.config.common.TokenInterceptor;
import com.bdq.common.entity.UserContext;
import com.bdq.common.utils.UserContextUtil;
import com.bdq.interaction.application.model.cmd.*;
import com.bdq.interaction.application.service.InteractionAppCmdService;
import com.bdq.interaction.domain.model.dto.req.*;
import com.bdq.interaction.domain.service.InteractionDomainService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author malei
 * @date 2024-05-14 15:00
 */
@Service
public class InteractionAppCmdServiceImpl implements InteractionAppCmdService {

    @Resource
    private InteractionDomainService interactionDomainService;

    @Override
    public Boolean interactionNumInit(InteractionNumInitCmd cmd) {
        return interactionDomainService.interactionNumInit(Convert.convert(InteractionNumInitReqDTO.class, cmd));
    }

    @Override
    public Boolean voteInit(VoteInitCmd cmd) {
        return interactionDomainService.voteInit(Convert.convert(VoteInitReqDTO.class, cmd));
    }

    @Override
    public Boolean view(ViewCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        if (Objects.isNull(userContext)) {
            return true;
        }

        // 2 浏览
        ViewReqDTO viewReqDTO = Convert.convert(ViewReqDTO.class, cmd);
        viewReqDTO.setUserId(userContext.getId());
        interactionDomainService.view(viewReqDTO);
        return true;
    }

    @Override
    public Boolean like(LikeCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());

        // 2 点赞
        LikeReqDTO likeReqDTO = Convert.convert(LikeReqDTO.class, cmd);
        likeReqDTO.setUserId(userContext.getId());
        interactionDomainService.like(likeReqDTO);
        return true;
    }

    @Override
    public Boolean cancelLike(LikeCancelCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        // 2 取消点赞
        LikeCancelReqDTO likeCancelReqDTO = Convert.convert(LikeCancelReqDTO.class, cmd);
        likeCancelReqDTO.setUserId(userContext.getId());
        interactionDomainService.cancelLike(likeCancelReqDTO);
        return true;
    }

    @Override
    public Boolean collect(CollectCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());

        // 2 收藏
        CollectReqDTO collectReqDTO = Convert.convert(CollectReqDTO.class, cmd);
        collectReqDTO.setUserId(userContext.getId());
        interactionDomainService.collect(collectReqDTO);
        return true;
    }

    @Override
    public Boolean cancelCollect(CollectCancelCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        // 2 取消收藏
        CollectCancelReqDTO collectCancelReqDTO = Convert.convert(CollectCancelReqDTO.class, cmd);
        collectCancelReqDTO.setUserId(userContext.getId());
        interactionDomainService.cancelCollect(collectCancelReqDTO);
        return true;
    }

    @Override
    public Boolean signUp(SignUpCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        // 2 报名
        SignUpReqDTO signUpReqDTO = Convert.convert(SignUpReqDTO.class, cmd);
        signUpReqDTO.setUserId(userContext.getId());
        interactionDomainService.signUp(signUpReqDTO);
        return true;
    }

    @Override
    public Boolean vote(VoteCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        // 2 投票
        VoteReqDTO voteReqDTO = Convert.convert(VoteReqDTO.class, cmd);
        voteReqDTO.setUserId(userContext.getId());
        interactionDomainService.vote(voteReqDTO);
        return true;
    }

    @Override
    public Boolean comment(CommentCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        // 2 评论
        CommentReqDTO commentReqDTO = Convert.convert(CommentReqDTO.class, cmd);
        commentReqDTO.setCommentUserId(userContext.getId());
        commentReqDTO.setCommentUserNickName(userContext.getNickName());
        commentReqDTO.setCommentUserAvatar(userContext.getAvatar());
        interactionDomainService.comment(commentReqDTO);
        return true;
    }
}
